// pages/tabBar/index/index.js
import tui from './../../common/httpRequest';
import {Encrypt} from '/../../utils/encrpt.js'
// import { hexMD5 } from './../../utils/md5';
import {
  validPhone
} from "../../utils/util.js";
const rules = [{
  name: "account",
  rule: ["required"],
  msg: ["请输入登录账号"]
},{
  name: "pwd",
  rule: ["required"],
  msg: ["请输入登录密码"]
}];
const rule = [{
  name: "accounts",
  rule: ["required", "isAccount"],
  msg: ["请输入登录手机号", "请输入正确格式的手机号"]
},{
  name: "code",
  rule: ["required"],
  msg: ["请输入密码"]
}];
const form = require("./../../components/common/tui-validation/tui-validation.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    bgImg: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/ic_loginbg.png',
    // logoImg: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/ic_logo.png',
    height: '',
    //表单数据
    formData: {
      accounts: "",
      code: ""
    },
    phones:'',
    code:'',
    yzmBtnText:'获取验证码',
    countdown:60,
    isfasong:true,//是否可以验证码状态
    isShowFlag:false, //是否显示验证码
    //校验规则
    rules: rules,
    loading: false,
    disabled: true,
  
    top: '',
    phoneImg: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/ic_signPhone.png',
    iconImg: ' https://image.anji-navinfo.cn/group1/M00/9F/9F/img/ic_signCode.png',
    currentTab: 0   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const capsuleInfo = wx.getMenuButtonBoundingClientRect();
    const systemInfo = wx.getSystemInfoSync(); // 获取设备信息
    const statusHeight = systemInfo.statusBarHeight // 状态栏高度
    this.setData({
      height: capsuleInfo.height ,
      top: capsuleInfo.top+statusHeight
    })
  },
  //登录账号
  getAcoount:function(e){
    if (e.detail.length == 11) {
    this.getCheckPhone(e.detail);
    }else{
      this.setData({
        disabled: true,
      })
    }
  },
  // blur(e){
  //   if (!validPhone(e.detail.detail.value)) {
  //     return tui.toast('手机号格式错误!');
  //   }
  // },
  //验证账号
  getCheckPhone: function(phone){
    if (!validPhone(phone)) {
      return tui.toast('手机号格式错误!');
    }
  },
   // 快速登录,输入手机号11位自动验证手机号
  getInputValue:function(e){
    if (e.detail.length == 11) {
      this.getCheckPhone(e.detail);
      this.setData({
        phones:e.detail
      })
    }else{
      this.setData({
        disabled: true,
        isShowFlag:false,
        isfasong: true,
        yzmBtnText: "获取验证码"
      })
    }
    // if(e.detail.length == 11){
    //   if(!validPhone(e.detail)){
    //     tui.toast('请检查手机号是否正确!');
    //     this.setData({
    //       isShowFlag: false
    //     })
    //   }else{
    //     tui.request('/checkPhone','GET',{mobile:e.detail}).then(res=> {
    //       if(res.Code === 2000){
    //         this.setData({
    //           phones:e.detail,
    //           isShowFlag:true
    //         })
    //       }else{
    //         tui.toast(res.Message)
    //         this.setData({
    //           isShowFlag:false
    //         })
    //       }
    //     })
    //   }
    // }
  },
 
  //密码登录 // 快速登录 
  submit(e) {
  // 验证码登录
  this.setData({
    formData: e.detail.value
  }, () => {
    let checkRes = form.validation(this.data.formData, rule,false);
    console.log(checkRes)
    if (!checkRes) {
      if (!validPhone(this.data.formData.accounts)) {
        return tui.toast('手机号格式错误!');
      }
        this.setData({
          loading: true,
          disabled: true
        }, () => {
          tui.request('sys/user/login', 'POST', {
            account: this.data.formData.accounts,
            password: Encrypt(this.data.formData.code)
          }, true).then(res => {
            if (res.code === 20000) {
              console.log(res)
              tui.setToken(res.data.token);
              wx.setStorageSync('account', res.data.account);
              // wx.setStorageSync('companyName', res.data.companyName);
              wx.setStorageSync('name', res.data.fullname);
              wx.setStorageSync('userType', res.data.accountType);
              
              wx.setStorageSync('department', res.data.department);
              wx.switchTab({
                url: '/pages/tabBar/index/index',
              })
            }else{
              tui.toast(res.message)
            }
            this.setData({
              loading: false,
              disabled: false
            })
          }).catch(()=>{
            this.setData({
              loading: false,
              disabled: false
            })
          })
        })

    } else {
      //验证不通过，checkRes为返回提示信息
      tui.toast(checkRes)
    }
  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


})